﻿Public Class F20_Them_Phieu_Thu

    Private Sub btnthoatpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoatpt.Click
        Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim f As New fr_Loaithu
        f.ShowDialog()
    End Sub

    Private Sub btnthempt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthempt.Click
        Try
            Dim p As PhieuThu = New PhieuThu()
            p.Ngay = dtpngaypt.Value.Date
            p.SoTien = txtsotienpt.Text
            p.LoaiThu = cbxloaithunhappt.SelectedValue
            p.TaiKhoan = cbxtaikhoanpt.SelectedValue
            p.NguoiThu = cbxnguoithu.SelectedValue
            p.NguoiLap = cbxnguoitao.SelectedValue
            p.ChiTiet = rtbchitiet.Text
            Dim kq As Integer = XL_PhieuThu.Them(p)
            If kq = 1 Then
                MessageBox.Show("Thêm thành công phiếu thu!")
            Else
                MessageBox.Show("Thêm không thành công phiếu thu!")
            End If
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End Try
        
    End Sub

    Private Sub F20_Them_Phieu_Thu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbxloaithunhappt.DataSource = XL_LoaiThu.LayDS()
        cbxloaithunhappt.DisplayMember = "Loai"
        cbxloaithunhappt.ValueMember = "ID"

        cbxnguoitao.DataSource = XL_NguoiDung.LayDS()
        cbxnguoitao.DisplayMember = "HoTen"
        cbxnguoitao.ValueMember = "ID"

        cbxnguoithu.DataSource = XL_NguoiDung.LayDS()
        cbxnguoithu.DisplayMember = "HoTen"
        cbxnguoithu.ValueMember = "ID"

        cbxtaikhoanpt.DataSource = XL_TaiKhoan.LayDS(Me.Tag)
        cbxtaikhoanpt.DisplayMember = "TenTK"
        cbxtaikhoanpt.ValueMember = "ID"

        cbxnguoitao.SelectedValue = Me.Tag
        cbxnguoithu.SelectedValue = Me.Tag
    End Sub

    Private Sub F20_Them_Phieu_Thu_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        For Each ctrl As Control In Me.Controls
            If ctrl.Focused = True And ctrl.GetType() Is GetType(Button) Then
                Select Case ctrl.Text
                    Case "Thêm"
                        btnthempt.PerformClick()
                    Case "Soạn lại"
                        btnxoapt.PerformClick()
                    Case "Thoát"
                        btnthoatpt.PerformClick()
                End Select
            End If
        Next
    End Sub

    Private Sub btnxoapt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoapt.Click
        dtpngaypt.Value = Now.Date
        txtsotienpt.Text = ""
        rtbchitiet.Text = ""
        cbxloaithunhappt.SelectedIndex = 0
        cbxnguoitao.SelectedIndex = 0
        cbxnguoithu.SelectedIndex = 0
        cbxtaikhoanpt.SelectedIndex = 0
    End Sub

    Private Sub txtsotienpt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsotienpt.TextChanged
        Try
            'RemoveHandler txtsotienpt.TextChanged, AddressOf txtsotienpt_TextChanged
            txtsotienpt.Text = FormatNumber(txtsotienpt.Text, 0)
            txtsotienpt.Select(txtsotienpt.Text.Length, txtsotienpt.Text.Length)
            'AddHandler txtsotienpt.TextChanged, AddressOf txtsotienpt_TextChanged
            'txtsotienpt.Tag = txtsotienpt.Tag * 10 + Integer.Parse(txtsotienpt.Text.Substring(txtsotienpt.Text.Length - 1))
        Catch ex As Exception
            'MessageBox.Show("Nhập lại thông tin", "Thông báo")
            'txtsotienpt.Text = ""
            'AddHandler txtsotienpt.TextChanged, AddressOf txtsotienpt_TextChanged
        End Try
    End Sub
End Class